package com.komodo.island.gdx.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.komodo.island.gdx.Art;
import com.komodo.island.gdx.Input;
import com.komodo.island.gdx.KomodoIsland;
import com.komodo.island.gdx.Persistence;

public class StoryScreen extends Screen {

	public int currScene;
	public int sceneNumber;
	public TextureRegion scene[];
	public Screen nextScreen;

	public double startTime;
	public double currTime;
	public double interval;

	public StoryScreen(TextureRegion scene[], int sceneNumber, double interval,
			Screen nextScreen) {
		// TODO Auto-generated constructor stub
		this.scene = scene;
		this.nextScreen = nextScreen;
		this.ID = Input.SCREEN_STORY;
		this.sceneNumber = sceneNumber;
		this.interval = interval;
	}

	public StoryScreen(TextureRegion scene[], int sceneNumber, Screen nextScreen) {
		// TODO Auto-generated constructor stub
		this.scene = scene;
		this.nextScreen = nextScreen;
		this.ID = Input.SCREEN_STORY;
		this.sceneNumber = sceneNumber;
		this.interval = 4000;
	}

	@Override
	public void init(KomodoIsland komodoIsland, Texture texture) {
		// TODO Auto-generated method stub
		super.init(komodoIsland, texture);
		currScene = 0;
	}

	@Override
	public void onScreenRun() {
		// TODO Auto-generated method stub
		if (currScene < sceneNumber) {
			currTime = System.currentTimeMillis();
			if (currTime - startTime <= interval) {
				if (scene != null) {
					batch.begin();
					batch.draw(scene[currScene], 0, 0, 480, 320);
					batch.end();
				}
				// Art.drawString(currScene + "", 0, 0);

				if (Gdx.input.justTouched()) {
					if (Persistence.sound)
						komodoIsland.dialogSound.play(1);
					startTime = currTime;
					currScene++;
				}

			} else {
				startTime = currTime;
				currScene++;
			}
		} else {
			komodoIsland.goToScreen(nextScreen);
		}

	}

	@Override
	public void onScreenLoading() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		startTime = System.currentTimeMillis();
		if (Persistence.sound) {
			if (komodoIsland.main.isPlaying()){
				komodoIsland.main.stop();
			}else{
				
			}
			if (komodoIsland.puzzle.isPlaying()){
				
			}else{
				komodoIsland.puzzle.play();
			}
			
		} else {
			komodoIsland.main.stop();
			if (komodoIsland.puzzle.isPlaying())
				komodoIsland.puzzle.stop();

		}
	}

	public void restartStory() {
		currScene = 0;
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
		restartStory();
		try{
			if (Persistence.sound){
				if (komodoIsland.puzzle.isPlaying()){
					komodoIsland.puzzle.stop();
				}
			}else{
				
			}
		}catch(Exception e){
			
		}
	}
}
